<template>
	<view>
		<view class="box">
			<view class="handpic">
				<image style="width: 175upx;height: 210upx;" :src="logo" mode=""></image>
			</view>
			<view class="loginflex">
				<view class="privacy-title">
					隐私保护提示
				</view>
				<view class="privacy-content">
					<p>感谢您信任并使用慧智康！</p>
					<p>我们非常重视您的个人信息和隐私保护。在您使用我们产品和服务之前，请充分阅读并理解完整的
						<text class="agreement-text" @tap="goAgreement(1)">《用户服务协议》</text>及<text @tap="goAgreement(2)"
							class="agreement-text">《隐私政策》</text>
						，特向您说明如下：
					</p>

					<p>1.为向您提供满足业务需要的相关基本功能，我们会收集、使用必要的信息；</p>
					<p>2.我们采取了互联网业内标准的技术措施和数据安全措施来保护您的个人信息安全；</p>
					<p>3.请确认您为18岁及以上人士。本产品不适用于未成年人；</p>
					<p>4.为方便您的查阅，可在登录小程序后，按路径“我的-设置-用户服务协议-隐私政策条款”中查看完整版隐私政策内容；</p>


				</view>

				<view class="but" @tap.stop="getUserData()">
					授权登录
				</view>
			</view>
			<u-popup v-model="isBindPhone" mode="center" border-radius="20">
				<view class="phone-login">
					<view class="title-login">
						请授权手机号进行登录哦！
					</view>
					<button class="but" open-type="getPhoneNumber"
						@getphonenumber="onGetPhoneNumber">授权手机号登录</button>
				</view>
			</u-popup>
			<view class="agreement">
				<view class="">
					<dev-svg-image @tap="dailyReading=false" v-show="dailyReading" svgName="choose"
						modify-color="#2EA7E0"></dev-svg-image>
					<dev-svg-image @tap="dailyReading=true" v-show="!dailyReading" svgName="noChoose"></dev-svg-image>
				</view>
				<view class="reading-text">
					<text class="an">请在使用慧智康前查看并同意完整的 </text>
					<text class="hot" @tap="goAgreement(1)">《用户服务协议》</text>
					<text>及</text>
					<text class="hot" @tap="goAgreement(2)">《隐私政策》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>

import uPopup from "uview-ui/components/u-popup/u-popup.vue";
import { init, updateSGUserInfo } from '@/sg/index'

	export default {
		components: {
			uPopup,
		},
		data() {
			return {
				logo: '',
				brandName: '',
				userInfo: [],
				code: '', //微信登录code码
				encryptedData: '',
				iv: '',
				isBindPhone: false, //绑定手机号弹框
				version_number: '', //版本号
				loginType: 1, //直接登录 2 注册

				dailyReading: false, //是否阅读协议

				jumpInterface: '', //登录成功后跳转到界面
				transferParameters: {}, //传递的参数
			};
		},
		onLoad(e) {
            console.log(e, '登录页面携带参数')
			if (e.jumpInterface) {
				this.jumpInterface = e.jumpInterface;
			}
			if (e.transferParameters) {
				this.transferParameters = JSON.parse(e.transferParameters);
			}
			this.logo = global.setting.logo //获取当前版本logo
			this.brandName = global.setting.brandName //当前公司名称
			// console.log(uni.getStorageSync('USER_TOKEN_BSY_Z'), 'onlaunch')
			const accountInfo = wx.getAccountInfoSync();
			this.version_number = accountInfo.miniProgram.version // 小程序 版本号
			console.log(this.version_number, '获取版本号')
		},
		methods: {
			navigateTo(e) {
				if (e == 'up') {
					uni.navigateBack({
						delta: 1,
						fail: function() {
							uni.reLaunch({
								url: '/pages/tab/home',
							});
						}
					})
				}
			},
			// 获取用户信息
			getUserData() {
				if (!this.dailyReading) {
					this.$message.info('请阅读，并同意隐私政策')
					return
				}
				uni.showLoading({
					title: '授权中'
				});
				let _this = this;
				uni.getUserProfile({
					desc: '用户登录',
					lang: 'en',
					success: function(userInfo) {
						console.log(userInfo);
						_this.userInfo = userInfo.userInfo;
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes);
								_this.code = loginRes.code;
								_this.login();
								uni.showLoading({
									title: '登录中'
								});
							},
							fail: function(err) {
								console.log(err)
							}
						});
					},
					fail: function(err) {
						uni.hideLoading();
						_this.$message.info('取消授权登录')
						console.log(err, '什么错')
					}
				});
			},
			//获取手机号授权
			onGetPhoneNumber(e) {
				let _this = this;
				// this.isBindPhone = false;
				console.log(e, '获取信息')
				_this.encryptedData = e.detail.encryptedData
				_this.iv = e.detail.iv
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes, 'loginRes11111');
							_this.code = loginRes.code;
							_this.login();
							uni.showLoading({
								title: '登录中'
							});
						},
						fail: function(err) {
							console.log(err)
						}
					});
				} else {
					uni.showToast({
						title: '请重新获取',
						icon: "none"
					})
				}
			},
			// 健康数据裂变
			saveByq(newUserFlag) {
				let extenUserId = uni.getStorageSync('extenUserId'),
					deviceTypeYq = uni.getStorageSync('deviceTypeYq')
				if (extenUserId && deviceTypeYq) { //邀请人 和 分享code 都有

					this.$api.post(global.apiUrls.saveByq, {
						extenUserId: extenUserId, //邀请人id
						newUserFlag: newUserFlag ? newUserFlag : 0, //这个页面默认不是新用户
						deviceType: deviceTypeYq //邀请类型
					}).then(res => { //邀请成功
						if (res.data.code == 1000) {
							uni.showToast({
								title: '加入成功',
								duration: 3000
							});
							if (extenUserId) {
								uni.removeStorageSync('extenUserId')
							}
							if (deviceTypeYq) {
								uni.removeStorageSync('deviceTypeYq')
							}
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none',
								duration: 3000
							});
						}
					})
				}
			},
			//请求登录
			login() {
				let _this = this,
					sendData = {
						avatarUrl: _this.userInfo.avatarUrl, //头像
						code: _this.code, //微信登录code
						gender: _this.userInfo.gender, //性别
						nickName: _this.userInfo.nickName, //昵称
						city: _this.userInfo.city, //微信所在的城市
						country: _this.userInfo.country, //微信所在的国家
						language: _this.userInfo.language, //微信用户语言
						province: _this.userInfo.province, //微信用户所在省份
						iv: _this.iv, //手机号密文
						encryptedData: _this.encryptedData, //手机号密文
						userSource: 1, //用户类型 1:C端用户  2:员工
						// versionNumber:_this.version_number,
						// appType:0,//0 小程序 1app
						loginType: _this.loginType, //1第一次登录 2当前用户不存咋进行注册
					};
				if (uni.getStorageSync('yqcode')) { //专属code
					sendData.inviteCode = uni.getStorageSync('yqcode')
				}
				if (uni.getStorageSync('kxjCode')) { //邀请康享家code
					sendData.inviteCode = uni.getStorageSync('kxjCode')
				}
				console.log(sendData.inviteCode, 1399999999999999)
				this.$loginl.post(global.apiUrls.login, sendData)
					.then(res => {
						console.log(res.data)
						if (res.data.code == 1000) {
							if (uni.getStorageSync('yqcode')) { //专属code
								uni.removeStorageSync('yqcode')
							}
							// 登陆成功
							global.token = res.data.result.accessToken;
							global.userInfo = res.data.result;
							let loginKey = global.IS_DEV?'USER_TOKEN_BSY_T':'USER_TOKEN_BSY_Z'
							uni.setStorageSync(loginKey, res.data.result.accessToken); //TY加TY 为测试版 不加为正式版
							uni.setStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z', res.data.result);
							uni.hideLoading()
							_this.getBaseUserInfo()
							setTimeout(() => {
								init(res.data.result.userId);
							}, 100);
							_this.saveByq(res.data.result.firstEntry) //健康数据裂变
							let pageurl = uni.getStorageSync('upPageurl');
							let firstEntry = res.data.result.firstEntry
                              console.info(this.jumpInterface,"this.jumpInterface登录成功后的参数1")
							if(firstEntry == 1) {
								uni.redirectTo({
									url: '/pages/signIn/accountsetting',
									params: {
										jumpInterface: this.jumpInterface,
										transferParameters: this.transferParameters
									}
								})
								return;
							}
							if (this.jumpInterface) {
                                console.info(this.transferParameters,"this.transferParameters登录成功后的参数2")
								this.$urouter.redirectTo({
									url: this.jumpInterface,
									params: this.transferParameters
								})
								return
							}

							if (pageurl) { //默认跳回上一页
								if (pageurl == '/pages/tab/home' && firstEntry != 1 || pageurl == '/pages/tab/find' &&
									firstEntry != 1 || pageurl == '/pages/tab/college' && firstEntry != 1 || pageurl ==
									'/pages/tab/my' && firstEntry != 1 || pageurl == '/pages/tab/makerT' &&
									firstEntry != 1 || pageurl == '/pages/tab/health' &&
									firstEntry != 1) {
									uni.reLaunch({
										url: pageurl
									})
								} else if (pageurl != '/pagesMy/my/relevance/share' && pageurl !=
									'/pagesMy/my/interconnect/interconnect' && firstEntry == 1 && pageurl !=
									'/pages/signIn/signinPages' && pageurl !=
									'/pages/subpackage/home/healthData/healthData') { //签到 家人邀请
									if (uni.getStorageSync('kxjCode')) { //邀请康享家
										uni.redirectTo({
											url: '/subpackage/maker/maker'
										});
									} else {
										uni.redirectTo({
											url: '/pages/subpackage/home/oneAccess/oneAccess'
										});
									}
								} else {
									uni.redirectTo({
										url: pageurl + '?signIn=1'
									});
								}
							} else { //为空情况
								uni.reLaunch({
									url: '/pages/tab/home'
								});
							}
						} else if (res.data.code == 5012) {
							uni.hideLoading()
							this.$message.info(res.data.message)
						} else if (res.data.code == 5204) {
							uni.hideLoading()
							_this.loginType = 2;
							_this.isBindPhone = true;
						} else {
							uni.hideLoading()
							this.$message.info('连接失败请稍后重试')
						}
					}).catch(err => {
						console.log(err)
					})
			},
			getBaseUserInfo() {
				let _this = this;
				this.$loginl.get(global.apiUrls.getBaseUserInfo + '?userId=' + uni.getStorageSync(
						global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId, {})
					.then(res => {
						if (res.data.code == 1000) {

							let result = res.data.result
							_this.updateUser(uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').avatarUrl, result.sex, result.nickName,
								result.remark)
						}
					}).catch(err => {})
			},
			//替换数据
			updateUser(avatarUrl, gender, nickName, remark) {
				let _this = this;
				let fdata = {
					avatarUrl: avatarUrl, //头像
					sex: gender, //性别
					nickName: nickName, //用户昵称
					userId: uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId,
					remark: remark ? remark : '' //签名
				}

				updateSGUserInfo({
					gender: gender,
					weight: 60,
					height: 175,
					age: 0,

				});
				this.$loginl.put(global.apiUrls.updateUser, fdata)
					.then(res => {}).catch(err => {})
			},
			//跳转到用户协议界面
			goAgreement(type) {
				this.$urouter.navigateTo({
					url: '/pages/signIn/agreement',
					params: {
						type: type
					}
				});
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.loginflex {
		display: flex;
		flex-direction: column;
		// justify-content: center;
		align-items: center;
		margin-top: 28upx;
		width: 658rpx;
		background-color: #FFFFFF;
		padding: 32rpx;
		box-shadow: 0rpx 2rpx 20rpx 0rpx rgba(173, 173, 173, 0.25);
		border-radius: 50rpx;

		.privacy-title {
			font-size: 36rpx;
			font-weight: 400;
			color: #000000;
		}

		.privacy-content {
			margin-top: 30rpx;
			height: 41vh;
			overflow: auto;
			font-size: 26rpx;
			font-weight: 400;
			color: #000000;
			line-height: 40upx;
		}

		.loginflexup {
			width: 200upx;
			height: 88upx;
			text-align: center;
			line-height: 88upx;
			background-color: #ccc;
			border-radius: 50upx;
			margin-right: 20upx;
			font-weight: bold;
			color: #fff;
		}
	}

	.xian {
		width: 100%;
		height: 1rpx;
		background: #EEEEEE;
		opacity: 1;
		margin-top: 32upx;
	}

	.box {
		// display: flex;
		// align-items:center;
		// justify-content: center;
		// flex-direction: column;

		min-height: 100vh;
		width: 100%;
		padding: 56upx;
		background-image: url('http://static.bsyjk.cn/login/EF382ABB7C6E41E4AE84E0BC9072A914.png');
		background-size: 100% 70vh;
		background-repeat: no-repeat;

		.handpic {
			width: 175upx;
			height: 210upx;
			margin: 6vh auto 0;
		}

		// 绑定手机号
		.phone-login {
			background-color: #FFFFFF;
			width: 600upx;
			height: 400upx;
			padding: 1upx;
			border-radius: 40upx;

			.but-boxs {
				margin-top: 90upx;
				width: 400upx;
				height: 90upx;
				border-radius: 45upx;
				// background: linear-gradient(112deg, #81C042 0%, #2EA7E0 100%);
				background: url(https://bsyjk-pic.bsyjk.cn/toprbg/10099E8526734AC383D6ADEED897F016.png)no-repeat;
				background-size: 750upx 375upx;
				font-size: 32upx;
				color: #FFFFFF;
				border: none;
			}

			.title-login {
				width: 600upx;
				text-align: center;
				height: 200upx;
				line-height: 200upx;
				font-size: 32upx;
				color: #333333;
			}
		}

		.but {
			margin-top: 30rpx;
			width: 466upx;
			height: 80upx;
			background: linear-gradient(112deg, #54B299 0%, #2EA7E0 100%);

			opacity: 1;
			border-radius: 44upx;
			text-align: center;
			font-size: 32upx;
			font-family: PingFang SC;
			font-weight: bold;
			line-height: 88upx;
			color: #FFFFFF;
			opacity: 1;
		}

		.agreement {
			position: fixed;
			width: 638upx;
			text-align: center;
			bottom: 60upx;
			display: flex;
			justify-content: space-between;

			.reading-text {
				width: 580rpx;
				text-align: left;
			}

			.an {
				height: 40upx;
				font-size: 28upx;
				font-family: PingFang-SC-Regular;
				line-height: 40upx;
				color: #999999;
				opacity: 1;
			}

			.hot {
				height: 40upx;
				font-size: 28upx;
				font-family: PingFang SC;
				line-height: 40upx;
				color: #2EA7E0;
				opacity: 1;
			}


		}


		.agreement-text {
			height: 40upx;
			font-size: 26upx;
			font-family: PingFang SC;
			color: #2EA7E0;
			opacity: 1;
		}

	}
</style>
